package com.bit.librarymanagementsystem.Service;

import com.bit.librarymanagementsystem.DAO.BookDAO;
import com.bit.librarymanagementsystem.Entity.BookInfo;
import com.bit.librarymanagementsystem.enums.BookStatus;
import com.bit.librarymanagementsystem.mapper.BookMapper;
import com.bit.librarymanagementsystem.model.PageRequest;
import com.bit.librarymanagementsystem.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookService {

    @Autowired
    BookMapper bookMapper;

    public List<BookInfo> getList(){
        BookDAO bookDAO=new BookDAO();
        List<BookInfo> bookList=bookDAO.mockData();
        for(BookInfo book:bookList){//设置中文状态码
            if(book.getStatus()==1){
                book.setStatusCN("可借阅");
            }else {
                book.setStatusCN("不可借阅");
            }
        }
        return bookList;
    }

    public void addBook(BookInfo bookInfo) {
        bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
       Integer count= bookMapper.count();
       List<BookInfo> books=bookMapper.queryListByPage(pageRequest);
        for(BookInfo book:books){//设置中文状态码
          book.setStatusCN(BookStatus.getDescByCode(book.getStatus()).getDesc());
        }
        return new PageResult<>(count,books,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
       return bookMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
       return bookMapper.updateBook(bookInfo);

    }

    public Integer deleteBook(Integer bookId) {
        BookInfo info=new BookInfo();
        info.setId(bookId);
        info.setStatus(0);
        return bookMapper.updateBook(info);
    }

    public Integer batchDeleteBook(List<Integer> ids) {
        return bookMapper.batchUpdateBook(ids);
    }
}
